﻿/********************************************************************
* Copyright (C) 2012 iTouch, Inc (http://www.itouch.com.cn) 
* All rights reserved
* Created by ZhuDongDong <dongdong_82975389@163.com> at 2012-3-30 0:37:45

* 版权所有：浙大网新易盛网络通讯有限公司
* 文件名称：XmlSerializer
* 作    者：朱冬冬(dongdong_82975389@163.com)
* 创建时间：2012-3-30 0:37:45
* 说    明：
*
********************************************************************
$Id: XmlSerializer.cs 27 2012-03-30 01:26:20Z dongdong82975389@gmail.com $
$Rev: 27 $ 
$Author: dongdong82975389@gmail.com $ 
$Date: 2012-03-30 01:26:20 +0000 (Fri, 30 Mar 2012) $
*********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;
using IDb;

namespace Serialization
{
    /// <summary>
    /// 序列化为XML数据格式类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class XmlSerializer : ISerializer
    {
        /// <summary>
        /// 将对象序列化为Xml格式表示的字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string Serialize<T>(T obj)
        {
            //StringWriter writer = new StringWriter();
            //System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            //serializer.Serialize(writer, obj);
            //string result = writer.ToString();

            //writer.Close();
            //return result;

            //构造用于保存序列化后的DataContract对象的流
            using (MemoryStream contractDataStream = new MemoryStream())
            {
                //将DataContract对象序列化到流
                DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                dcs.WriteObject(contractDataStream, obj);

                //为了查看序列化到流中的内容,将流内容读取出来并用UTF8解码为字符串
                byte[] contractDataByte = new byte[contractDataStream.Length];
                contractDataStream.Position = 0;
                contractDataStream.Read(contractDataByte, 0, (int)contractDataStream.Length);
                string contractDataString = Encoding.UTF8.GetString(contractDataByte);
                return contractDataString;
            }
        }
        /// <summary>
        /// 将Xml格式表示的字符串还原为对象
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public T Deserialize<T>(string xml)
        {
            //StringReader strReader = new StringReader(xml);
            //XmlReader reader = new XmlTextReader(strReader);
            //System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            //T obj = (T)serializer.Deserialize(reader);

            //strReader.Close();
            //reader.Close();
            //return obj;

            byte[] bt = Encoding.UTF8.GetBytes(xml);
            using (MemoryStream contractDataStream = new MemoryStream(bt))
            {
                DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                T obj = (T)dcs.ReadObject(contractDataStream);

                return obj;
            }
        }
    }
}